// pages/my/play/play.js
const api = require('../../../api/index.js')
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    oderData: {}, // 当前订单数据
    currentMoney: 0, // 当前钱数
    grossPrice: "", // 总价
    countNum: 1, // 当前订单数量
    phone: '',
    kId: '',
    vId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    this.getPlayData(options)

    if (options.num) {

      this.setData({
        countNum: options.num
      })

    }
    

    
  },
  discountsState: function() {
    let kid = this.data.kId;
    let vId = this.data.vId;
    let discount_id = this.data.oderData.discount_id
    let mobmic = this.data.mobmic
    let num = this.data.countNum;
    wx.navigateTo({
      url: `/pages/my/PayCoupon/PayCoupon?kid=${kid}&vid=${vId}&discount_id=${discount_id}&mobmic=${mobmic}&num=${num}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let onloDan = wx.getStorageSync('onloDanD')
    let payOp = wx.getStorageSync('payOp')
    
    if ( onloDan == 1){
      this.getPlayData(payOp)
      console.log(payOp)
      wx.removeStorageSync('onloDanD')
    }
    
  },
  Coupon: function() {
    let that = this;
    if (that.data.oderData.type == 1) {
      let count = that.data.countNum * that.data.oderData.unit_price - that.data.oderData.full_price
      that.setData({
        grossPrice: count,
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

    wx.removeStorageSync('onloDanD')
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    app.globalData.Refresh = true;
    // let c_id = this.data.oderData.c_id;
    // app.globalData.IndexQuery = c_id;
    console.log(app.globalData.Refresh);
    // wx.switchTab({
    //   url:'../../../pages/lookVennue/lookVennue/lookVennue'
    // })



  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  count: function(e) {
    let money = this.data.currentMoney * 1000;
    let num = Number(e.detail);
    let payConpon = this.data.oderData.full_price;
    let count = ((money * num) / 1000) - payConpon;
    this.setData({
      grossPrice: count,
      countNum: e.detail
    })

  },
  getPlayData: function(options) { // 获取数据
    console.log(options)
    api.order(options, (res) => {
      console.log(res);
      let phone = res.data.result.user_mobile.substr(0, 3) + "****" + res.data.result.user_mobile.substr(7);
      let that = this;
      this.setData({
        oderData: res.data.result,
        currentMoney: res.data.result.unit_price,
        grossPrice: res.data.result.unit_price,
        phone: phone,
        kId: options.k_id,
        vId: options.v_id,
        mobmic: res.data.result.user_mobile
      })
      if (res.data.result.type == 2) {
        let cont = that.data.countNum * that.data.oderData.unit_price
        that.setData({
          grossPrice: cont
        })
      }
      this.Coupon();
    })
  },
  goPlay: function() {
    let orderSn = this.data.oderData.order_sn;
    let openid = wx.getStorageSync('openid').openid;
    console.log(openid)
    let discount_id = this.data.oderData.discount_id
    let data = {
      v_id: this.data.vId,
      k_id: this.data.kId,
      num: this.data.countNum,
      openid_wechat: openid,
      discount_id: discount_id
    }
    api.PaySelect(data, (res) => {
      console.log(res,'这是一个我要看的数据')
      let url = res.data.result.url
      let typePay = res.data.result.type
      data.url = url;
      if (typePay == 1) {
        api.play(data, (res) => {
           let orderSn = res.data.result;
          console.log(res,'返回给我的数据')
          wx.navigateTo({
            url: `/pages/my/orderDetails/orderDetails?order_id=${orderSn}&OrderType=${1}`,
          })
        })
      } else {
        api.play(data, (res) => {
          console.log(res,'这是返回给我的数据');
          let playData = res.data.result;
          let orderSn = playData.order_sn
          console.log(res.data.result)
          console.log(res)
          wx.requestPayment({
            'timeStamp': playData.timeStamp,
            'nonceStr': playData.nonceStr,
            'package': playData.package,
            'signType': playData.signType,
            'paySign': playData.paySign,
            'success': function(res) {
              App.zhuge.track('支付订单', {
                '支付成功': '支付成功'
              });
              console.log('交钱成功')
              console.log(orderSn);
              wx.navigateTo({
                url: `/pages/my/orderDetails/orderDetails?order_id=${orderSn}`,
              })

              console.log(res)
            },
            'fail': function(res) {
              App.zhuge.track('支付订单', {
                '支付失败': '支付失败'
              });
              wx.showToast({
                title: '支付失败',
                icon: 'none'
              })
              console.log('交钱失败')

              console.log(res)
            }
          })
          console.log(res)
        })


      }


    })


  }
})